package com.etime.shuhuiyuan.services.demo.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.shuhuiyuan.dao.DemoDao;
import com.etime.shuhuiyuan.pojo.Demo;
import com.etime.shuhuiyuan.services.demo.interfaces.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * demo示例service层实现类
 */
@Service
public class DemoServiceImpl implements DemoService {
    /**
     * 使用spring的ioc容器自动注入对象实例
     */
    @Autowired
    private DemoDao demoDao;
    /**
     * 添加示例
     *
     * @param demo
     * @return
     */
    @Override
    public int insertDemo(Demo demo) {
        return demoDao.insert(demo);
    }

    /**
     * 修改示例(根据id进行修改。)
     *
     * @param demo 参数中必须要有id
     * @return
     */
    @Override
    public int updateDemo(Demo demo) {
        return demoDao.updateById(demo);
    }

    /**
     * 查询列表示例
     *
     * @param demo
     * @return
     */
    @Override
    public List<Demo> selectDemoList(Demo demo) {
        //查询条件
        QueryWrapper<Demo> wrapper = new QueryWrapper<>();
        if(demo!=null && demo.getDemoname()!=null && !demo.getDemoname().equalsIgnoreCase("")) {
            wrapper.eq("demoname", demo.getDemoname());
        }
        if(demo!=null && demo.getDemostate()!=null && demo.getDemostate()!=-1){
            wrapper.eq("demostate",demo.getDemostate());
        }
        return demoDao.selectList(wrapper);
    }

    /**
     * 分页查询示例
     *
     * @param demo
     * @param pagenum
     * @param pagesize
     * @return
     */
    @Override
    public Page<Demo> selectDemoByPage(Demo demo, long pagenum, long pagesize) {
        //分页参数
        Page<Demo> page = new Page<>(pagenum,pagesize);
        //查询条件
        QueryWrapper<Demo> wrapper = new QueryWrapper<>();
        if(demo!=null && demo.getDemoname()!=null && !demo.getDemoname().equalsIgnoreCase("")) {
            wrapper.eq("demoname", demo.getDemoname());
        }
        if(demo!=null && demo.getDemostate()!=null && demo.getDemostate()!=-1){
            wrapper.eq("demostate",demo.getDemostate());
        }

        return demoDao.selectPage(page,wrapper);
    }

    /**
     * 根据id查询一条记录示例
     *
     * @param demoid
     * @return
     */
    @Override
    public Demo selectDemoByid(Integer demoid) {
        return demoDao.selectById(demoid);
    }
}
